package com.iafme.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.iafme.dto.res.CertificateOrderResDTO;
import com.iafme.dto.res.OrderResDTO;
import com.iafme.model.OrderNoType;
import com.iafme.service.CertificateOrderService;
import com.iafme.service.IOrderService;
import com.iafme.service.OrderNoTypeService;
import com.iafme.socket.PaySocketServer;
import com.systemframework.constants.Constants;
import com.systemframework.pay.service.impl.AlPay;
import com.systemframework.pay.service.impl.WxPay;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/8 10:05
 * 扫码支付回调
 */
@RestController
@RequestMapping(value = "/pay")
@AllArgsConstructor
@Slf4j
public class PayController {

    private final WxPay wxPay;

    private final AlPay alPay;

    private final IOrderService orderService;

    private final PaySocketServer paySocketServer;

    private final OrderNoTypeService orderNoTypeService;

    /**
     * 考试注册成功回调
     * @param xmlData
     */
    @PostMapping(value = "/wxNotify")
    public String wxNotify(@RequestBody String xmlData){
        log.info("微信支付回调:{}",xmlData);
        WxPayOrderNotifyResult wxPayOrderNotifyResult = wxPay.notifyResult(xmlData);
        String outTradeNo = wxPayOrderNotifyResult.getOutTradeNo();
        OrderNoType orderNoType = getOrderNoType(outTradeNo);
        OrderResDTO orderResDTO=new OrderResDTO();
        if(orderNoType.getType().equals(0)){
             orderResDTO=orderService.updateOrderStatus(outTradeNo);
        }else{
             orderResDTO=certificateOrderService.updateStatus(outTradeNo);
        }
        paySocketServer.sendMessage(orderResDTO);
        return WxPayNotifyResponse.success(Constants.SUCCESS);
    }

    private OrderNoType getOrderNoType(String orderNo){
        LambdaQueryWrapper<OrderNoType> eq = Wrappers.lambdaQuery(OrderNoType.class).eq(OrderNoType::getOrderNo, orderNo);
        return orderNoTypeService.getOne(eq);
    }

    @PostMapping(value = "/aliNotify")
    public void aliPay(HttpServletRequest request){
        log.info("支付宝支付回调:{}",alPay.notifyResult(request));
        Map<String, Object> map = alPay.notifyResult(request);
        String orderNo = Convert.convert(String.class, map.get("orderNo"));
        OrderNoType orderNoType = getOrderNoType(orderNo);
        OrderResDTO orderResDTO=new OrderResDTO();
        if(orderNoType.getType().equals(0)){
            orderResDTO=orderService.updateOrderStatus(orderNo);
        }else{
            orderResDTO=certificateOrderService.updateStatus(orderNo);
        }
        paySocketServer.sendMessage(orderResDTO);
    }

    /**
     * 证书支付成功回调
     */
    private final CertificateOrderService certificateOrderService;

    @PostMapping(value = "/certificateWxNotify")
    public void certificateWxNotify(@RequestBody String xmlData){
        WxPayOrderNotifyResult wxPayOrderNotifyResult = wxPay.notifyResult(xmlData);
        OrderResDTO orderResDTO=certificateOrderService.updateStatus(wxPayOrderNotifyResult.getOutTradeNo());
        paySocketServer.sendMessage(orderResDTO);
    }


    @PostMapping(value = "/certificateAliNotify")
    public void certificateAliNotify(HttpServletRequest request){
        Map<String, Object> map = alPay.notifyResult(request);
        OrderResDTO orderResDTO=certificateOrderService.updateStatus(Convert.convert(String.class, map.get("orderNo")));
        paySocketServer.sendMessage(orderResDTO);
    }

}
